import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { REQ_URL } from '../Constants'
import { HttpService } from '../httpService'
import { AppGlobal } from '../appGlobal'
import { JpushService } from '../jpushService';

/*
  Generated class for the UserProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class UserProvider {

    constructor(public http: Http,
        public httpService: HttpService,
        public platform: Platform,
        public jpushService: JpushService) {
        //console.log('Hello UserProvider Provider');
    }

    //通过用户名和密码登录
    loginByUsername(obj) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.user.login, obj)
                .then(data => {
                    AppGlobal.getInstance().setToken(data.token);
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }


    //通过token获取用户信息 token 是放在header中的
    getUserInfoByToken() {
        return new Promise((resolve, reject) => {
            // console.log(this.appGlobal.getToken());
            this.httpService.postBody(REQ_URL.user.loginByToken, AppGlobal.getInstance().getToken())
                .then(data => {
                    // console.log(data.userInfo);
                    AppGlobal.getInstance().setUserInfo(data.userInfo);
                    AppGlobal.getInstance().setBadTypes(data.alarmTypes);
                    AppGlobal.getInstance().setRoleResources(data.roleResources);
                    AppGlobal.getInstance().setRoleButtons(data.roleButtons);
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //更新的jpushCode
    updateJpushCode() {
        //TODO 重新设置 pushCode 
        //************************* START
        //首先判断是否platform，pushCode是否一致，不一致则更新 1-Android，2-ios

        var userInfo = AppGlobal.getInstance().getUserInfo();
        var pushObj = {
            id: userInfo.id,
            platform: userInfo.pushPlatform,
            pushCode: ''
        }
        if (this.platform.is("ios")) {
            pushObj.platform = '2'
        } else if (this.platform.is("android")) {
            pushObj.platform = '1'
        }
        this.jpushService.init().then(res => {

            this.jpushService.openListenEvent();
            //初始化成功 获取RID
            this.jpushService.getRegistrationID().then((res: any) => {
                //获取成功 比较pushCode 和 返回的是否一致
                if (userInfo.pushCode == res) {
                    return;
                } else {
                    pushObj.pushCode = res;

                    //重新更新pushCode
                    this.httpService.postBody(REQ_URL.user.updatePushCode, pushObj).then(res => {
                        console.log(res)
                    }, err => {
                        console.log(err);
                    });
                }
            }).catch(err => console.log(err))

        }).catch(err => console.log(err))
        //************************* END
    }


    //修改密码
    changePwd(obj) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.user.updatePassword, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }


    //获取通知标记
    getNoticeFlag(obj) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.user.loadNoticeFlag, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //设置通知标记
    setNoticeFlag(obj) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.user.saveNoticeFlag, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //获取app版本号
    getAppVersion(obj) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.app.getVersion, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    //更新app版本
    updateAppVersion(obj) {
        return new Promise((resolve, reject) => {
            this.httpService.postBody(REQ_URL.app.UpdateVersion, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }


}
